package com.bsi.conf;

import com.sun.org.apache.xpath.internal.operations.String;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @ClassName SpringSecurityConfigAdapter
 * @Author LiuSong
 * @Date 2020/8/11 15:46
 * @Version 1.0
 * @Description
 */
@EnableWebSecurity
public class SpringSecurityConfigAdapter extends WebSecurityConfigurerAdapter {

    /**
     * 创建校验器
     *
     * @return
     */
    @Bean
    @Override
    public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();
        userDetailsManager.createUser(User.withUsername("zhangsan").password("1234").authorities("r1").build());
        userDetailsManager.createUser(User.withUsername("lisi").password("1234").authorities("r2").build());
        return userDetailsManager;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }


    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/r/r1").hasAuthority("r1").
                antMatchers("/r/r2").hasAuthority("r2").
                antMatchers("/r/**").authenticated().
                anyRequest().permitAll().
                and().formLogin().successForwardUrl("/login-success");
    }
}
